<?php

/*数据类*/
class QuickMySQL
{
	public $mysql = null;	/*数据库对像*/
	/* 构造函数 */
	function __construct() {
		global $mysql_server, $mysql_user, $mysql_passwd, $mysql_database;
		$this->mysql = mysql_connect($mysql_server, $mysql_user, $mysql_passwd);
		if( !$this->mysql )
			return;
		
		mysql_select_db($mysql_database, $this->mysql);
	}
	/* 析构函数 */
	function __destruct() {
		if( $this->mysql )
			mysql_close($this->mysql);
	}
	/*开始事务*/
	function Begin() {
		if( !$this->mysql )
			return false;
		if( !mysql_ping($this->mysql) )
			return false;
			
		$null = mysql_query("START TRANSACTION", $this->mysql);
		return mysql_query("BEGIN", $this->mysql);
	}
	/*提交事务*/
	function Commit(){
		if( !$this->mysql )
			return false;
		if( !mysql_ping($this->mysql) )
			return false;
		
		return mysql_query("COMMIT", $this->mysql);
	}
	/*回液事务*/
	function Rollback(){
		if( !$this->mysql )
			return false;
		if( !mysql_ping($this->mysql) )
			return false;
			
		return mysql_query("ROLLBACK", $this->mysql);
	}
	/*查询*/
	function Query($sql){
		if( !$this->mysql )
			return false;
		if( !mysql_ping($this->mysql) )
			return false;
		
		return mysql_query($sql, $this->mysql);
	}
	/*获取表的记录数*/
	function getTableCount($table){
		if( !$this->mysql )
			return 0;
		if( !($res = mysql_query("select count(*) from {$table}", $this->mysql) ) )
			return 0;
		$ret = 0;
		$row = mysql_fetch_row($res);
		if( $row )
			$ret = (int)$row[0];
		mysql_free_result($res);
		return $ret;
	}
	/*获取个数 必须是select count(*) 这样的格式*/
	public static function getSelectCount($sql){
		$cnn = new QuickMySQL();
		if( !$cnn->mysql )
			return 0;
		if( !($res = mysql_query($sql, $cnn->mysql) ) )
			return 0;
		$ret = 0;
		$row = mysql_fetch_row($res);
		if( $row )
			$ret = (int)$row[0];
		mysql_free_result($res);
		return $ret;
	}
	/*快速执行*/
	public static function quickExecute($sql){
		$cnn = new QuickMySQL();
		if( !$cnn->mysql )
			return false;
		if( !mysql_query($sql, $cnn->mysql) )
			return false;
		return 0 == mysql_errno($cnn->mysql);
	}
}

?>